﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using ArlesInterfaces.Editor;
using Arles.src.controller;

namespace Arles.src.model
{
    public class Scene : IScene
    {
        List<ISceneObject> objects;
        Dictionary<ObjectType, object> dictionaryObjects;

        public List<ISceneObject> Objects
        {
            get { return objects; }
        }


        public Scene()
        {
            objects = new List<ISceneObject>();
            dictionaryObjects = new Dictionary<ObjectType, object>();
            dictionaryObjects.Add(ObjectType.Skydome, null);
            dictionaryObjects.Add(ObjectType.Terrain, null);
            dictionaryObjects.Add(ObjectType.Water, null);
            dictionaryObjects.Add(ObjectType.Vegetation, new List<ISceneObject>());
            dictionaryObjects.Add(ObjectType.Objects, new List<ISceneObject>());
        }

        public void AddObject(ObjectType type, ISceneObject newObject)
        {
            objects.Add(newObject);

            switch (type)
            {
                case ObjectType.Skydome:
                case ObjectType.Terrain:
                case ObjectType.Water:
                    dictionaryObjects[type] = newObject;

                    //Add View
                    Editor.Instance.MainForm.SceneTree.Nodes.Add(type.ToString(), type.ToString());
                    break;

                case ObjectType.Objects:
                case ObjectType.Vegetation:
                    ((List<ISceneObject>)dictionaryObjects[type]).Add(newObject);

                    //Add View
                    int index = ((List<ISceneObject>)dictionaryObjects[type]).Count - 1;
                    Editor.Instance.MainForm.SceneTree.Nodes.Add(type.ToString(), type.ToString() + index, index);

                    break;
            }
        }

        public ISceneObject GetObject(ObjectType type) { return GetObject(type, -1); }
        public ISceneObject GetObject(ObjectType type, int index)
        {
            ISceneObject result = null;

            switch (type)
            {
                case ObjectType.Skydome:
                case ObjectType.Terrain:
                case ObjectType.Water:
                    
                    result = dictionaryObjects[type] as ISceneObject;
                    break;

                case ObjectType.Objects:
                case ObjectType.Vegetation:
             
                    if (((List<ISceneObject>)dictionaryObjects[type]).Count > index && index > 0)
                        result = ((List<ISceneObject>)dictionaryObjects[type])[index];
                    
                    break;
            }

            return result;
        }

        public void DeleteObject(ObjectType type) { DeleteObject(type, -1); }
        public void DeleteObject(ObjectType type, int index)
        {
            //Comprobar si el objeto a borrar está siendo usado por el propertygrid
            if (Editor.Instance.MainForm.Properties.SelectedObject != null)
            {
                int selectedObject = Editor.Instance.MainForm.Properties.SelectedObject.GetHashCode();
                if(selectedObject.Equals(dictionaryObjects[type].GetHashCode()))
                    Editor.Instance.MainForm.Properties.SelectedObject = null;

            }

            //Borrar el objeto
            switch (type)
            {
                case ObjectType.Skydome:
                case ObjectType.Terrain:
                case ObjectType.Water:
                    if(dictionaryObjects[type] != null)
                    {
                        objects.Remove((ISceneObject)dictionaryObjects[type]);
                        dictionaryObjects[type] = null;
                        Editor.Instance.MainForm.SceneTree.Nodes.RemoveByKey(type.ToString());
                    }

                    break;

                case ObjectType.Objects:
                case ObjectType.Vegetation:

                    //TODO
                    throw new NotImplementedException();
                    break;
            }
        }
    }
}
